package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysMeetBo;
import com.ruoyi.system.domain.vo.SysMeetVo;
import com.ruoyi.system.domain.SysMeet;
import com.ruoyi.system.mapper.SysMeetMapper;
import com.ruoyi.system.service.ISysMeetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会议Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-17
 */
@RequiredArgsConstructor
@Service
public class SysMeetServiceImpl implements ISysMeetService {

    private final SysMeetMapper baseMapper;

    /**
     * 查询会议
     *
     * @param id 会议主键
     * @return 会议
     */
    @Override
    public SysMeetVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询会议列表
     *
     * @param bo 会议
     * @return 会议
     */
    @Override
    public TableDataInfo<SysMeetVo> queryPageList(SysMeetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMeet> lqw = buildQueryWrapper(bo);
        Page<SysMeetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会议列表
     *
     * @param bo 会议
     * @return 会议
     */
    @Override
    public List<SysMeetVo> queryList(SysMeetBo bo) {
        LambdaQueryWrapper<SysMeet> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMeet> buildQueryWrapper(SysMeetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysMeet> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysMeet::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysMeet::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysMeet::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增会议
     *
     * @param bo 会议
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysMeetBo bo) {
        SysMeet add = BeanUtil.toBean(bo, SysMeet.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会议
     *
     * @param bo 会议
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysMeetBo bo) {
        SysMeet update = BeanUtil.toBean(bo, SysMeet.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysMeet entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会议
     *
     * @param ids 需要删除的会议主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
